import numpy as np
import matplotlib.pyplot as plt

temperature_str = np.loadtxt('ug_detect.csv',
                             dtype=bytes,
                             delimiter=',',
                             skiprows=1,
                             usecols=(1),
                             unpack=False)
# print("读取出的数组是temperature_str：\n", temperature_str)

# 构造一个数组存储温度
temperature = np.ndarray(len(temperature_str))
for index in range(0, len(temperature_str)):
    item = temperature_str[index]
    if item != b'':
        item = item.decode('gb2312')
        item = float(item)
    else:
        item = None
    temperature[index] = item

# print("处理后的温度数据：", temperature)

# 处理异常值
for index in range(0, len(temperature)):
    item = temperature[index]
    if item>= 50.0:
        item = None
    temperature[index] = item
# print("处理后的温度数据：", temperature)

# t = np.arange(len(temperature))
# plt.plot(t, temperature)
# plt.plot(t, temperature, 'pr')
# plt.show()

# 用插值法处理缺失值
def bisec(dataArray):
    for index in range(0, len(dataArray)):
        if np.isnan(dataArray[index]):
            if (index-1)>=0 and (index+1)<len(dataArray):
                dataArray[index] = (dataArray[index-1] + dataArray[index + 1])*0.5
bisec(temperature)
print("处理完缺失值：", temperature)
t = np.arange(len(temperature))
plt.plot(t, temperature)
plt.plot(t, temperature, 'pr')
plt.show()

np.savetxt('ug_temperature.csv',
           temperature,
           delimiter=',',
           fmt='%.2f')









